<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="qq" />
            <span> 江苏传智播客教育科技股份有限公司 </span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree :data="list">
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <svg-icon icon-class="home" />
                <span> {{ data.name }} </span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item
                          v-if="data.children && data.children.length === 0"
                          @click.native="hDelete(data.id)"
                        >删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <el-dialog
        :title="isEdit ? '编辑部门' : '新增子部门'"
        :visible.sync="showDialog"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        @close="hClose"
      >
        <add-edit
          :id="curId"
          ref="myDept"
          :origin-list="originList"
          :is-edit="isEdit"
          @success="hSuccess"
          @close="showDialog = false"
        />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getDepartments, delDepartment } from '@/api/departments'
import addEdit from './deptDialog.vue'
import { tranListToTreeData } from '@/utils/index'
export default {
  components: {
    addEdit
  },
  data() {
    return {
      // 依赖一份树形数据
      showDialog: false,
      list: [],
      originList: [],
      curId: '',
      isEdit: false // false是新增 true是编辑
    }
  },
  created() {
    this.loadDepartment()
  },
  methods: {
    async loadDepartment() {
      try {
        const res = await getDepartments()
        console.log(res)
        this.originList = res.data.depts.map(item => {
          const { name, code, id, pid } = item
          return { name, code, id, pid }
        })
        // console.log(originList)
        res.data.depts.shift()
        this.list = tranListToTreeData(res.data.depts)
      } catch (error) {
        console.log(error)
      }
    },
    hAdd(id) {
      this.curId = id
      this.isEdit = false
      this.showDialog = true
    },
    hEdit(id) {
      this.curId = id
      this.isEdit = true
      this.showDialog = true
      this.$nextTick(() => {
        this.$refs.myDept.loadDepartmentDetail()
      })
    },
    hClose() {
      this.$refs.myDept.hCancel()
    },
    hDelete(id) {
      try {
        this.$confirm('你确定删除该部门吗?, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
          const res = await delDepartment(id)
          console.log(res)
          this.loadDepartment()
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      } catch (error) {
        console.log(error)
      }
    },
    hSuccess() {
      this.showDialog = false
      this.loadDepartment()
    }
  }
}
</script>
